<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\model\ShopTransfers as model;
use think\App;
use think\facade\Hook;

class ShopTransfers extends Base{
    public function __construct(App $app = null){
        parent::__construct($app);
        $this->model = new model;
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param string $sort
     * @param int $limit
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index($where=[],$sort='',$limit=8){
        $where = $this->_where($where);
        $order = $this->_order($sort);
        $where[]=["{$this->model->getTable()}.status",'neq',-1];
        $list = $this->model->getList($where,$order,$limit);
        return $this->__s('',$list);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function delete($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        $info->status = -1;
        if(!$info->save()){
            return $this->__e('删除失败');
        }
        $sql = $info->getLastSql();
        $data = [
            'aid'=>self::$uid,
            'action'=>"删除【{$this->model->getTableComment()}】，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$data);
        return $this->__s('删除成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    public function edit($id){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    /**
     * @param int $id
     * @param int $type
     * @return \think\Response
     */
    public function update($id=0,$type=1){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        $info->status = $type==1?4:5;
        if(!$info->save()){
            return $this->__e('操作失败');
        }
        $_info = $type==1?'上架':'下架';
        $sql = $info->getLastSql();
        $data = [
            'aid'=>self::$uid,
            'action'=>"{$_info}{$this->model->getTableComment()}【{$info['title']}】，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$data);
        return $this->__s('操作成功');
    }

    /**
     * 提现
     * @param int $id 订单id
     * @param int $type 1同意2不同意
     * @param string $info 拒绝理由
     * @route('admin/shop_goods_transfers/confirm','post')
     * @return \think\Response
     * @throws \Exception
     */
    public function confirm($id=0,$type=1,$info=''){
        $result = $this->model->confirm($id,$type,$info,$out);
        if(!$result){
            return $this->__e($out);
        }
        $_info = $type==1?'提现成功':'提现失败';
        $data = [
            'aid'=>self::$uid,
            'action'=>"{$_info}【{$out['order_id']}】，编号：{$id}",
            'sql'=>$out['sql']
        ];
        $payload = [
            'title'=>'商户提现',
            'content'=>$out['msg'],
            'payload'=>[
                'type'=> 8,
                'id'=>$out['id'],
                'cid'=> 8
            ]
        ];
        (new Push())->pushOne($out['uid'],$payload);
        Hook::listen('admin_log',$data);
        return $this->__s('操作成功');
    }
}